package com.example.safeassistan.home.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.adapter.CheckLogCommentFragmentCAdapter;
import com.example.safeassistan.home.model.CheckCommentPerson;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.utile.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class PersonCommentFragment extends BaseVPFragment {
    private Context context;
    private View view;
    private CheckLogCommentFragmentCAdapter adapter;

    private RecyclerView rcView;
    private LinearLayout llNodata;

    private String checkTableId="";//检查表id
    private String defId="";//检查记录表表id
    private String riskPointId="";//风险点id

    public static PersonCommentFragment newInstance(int  type,String checkTableId,String defId,String riskPointId) {
        Bundle arguments = new Bundle();
        arguments.putInt("type",type);
        arguments.putString("checkTableId",checkTableId);
        arguments.putString("defId",defId);
        arguments.putString("riskPointId",riskPointId);
        PersonCommentFragment fl = new PersonCommentFragment();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.checklog_personcomment_fragment, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        rcView=view.findViewById(R.id.rcView);
        llNodata=view.findViewById(R.id.llNodata);
        riskPointId=getArguments().getString("riskPointId");
        checkTableId=getArguments().getString("checkTableId");
        defId=getArguments().getString("defId");
    }


    public void getData(){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("checkTableId", checkTableId);
            jsonObject.put("defId",defId);
            jsonObject.put("riskPointId",riskPointId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new HttpRequest(context).doPost(UrlConstants.checkpersonScoreRecord, null,jsonObject ,new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean  success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), CheckCommentPerson.class);
                        if (obj instanceof CheckCommentPerson) {
                            CheckCommentPerson  riskPointDetail=(CheckCommentPerson) obj;
                                if (riskPointDetail.getResult().size() > 0) {
                                    llNodata.setVisibility(View.GONE);
                                    rcView.setVisibility(View.VISIBLE);
                                    adapter=new CheckLogCommentFragmentCAdapter(riskPointDetail.getResult());
                                    rcView.setLayoutManager(new LinearLayoutManager(context));
                                    rcView.setAdapter(adapter);
                                    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        }
                                    });
                                } else {
                                    llNodata.setVisibility(View.VISIBLE);
                                    rcView.setVisibility(View.GONE);
                                }
                        }
                    }else {
                        rcView.setVisibility(View.GONE);
                        llNodata.setVisibility(View.VISIBLE);
                        //Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    protected void onFragmentFirstVisible() {
        getData();
    }
}
